package com.dsz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.Expression;
import org.springframework.util.StringUtils;
import com.dsz.entity.Users;
import com.dsz.repository.UsersRepository;
import com.dsz.service.UsersService;
import com.dsz.until.FenyeUtil;
import com.dsz.until.GetDateUtils;
import com.dsz.until.MD5Util;

@Service
public class UsersServiceImpl implements UsersService {
	@Autowired
	private UsersRepository ur;
	
	@Override
	public Users login(Users u) {
		return ur.findUsersByLoginnameAndPassword(u.getLoginname(),u.getPassword());
	}

	@Override
	public List<Users> all() {
		return ur.findAll();
	}

	@Override
	public FenyeUtil<Users> getAll(FenyeUtil<Users> fy, Users users) {
		//创建Specification
				Specification<Users> spec = new Specification<Users>() {

					@Override
					public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query,
							CriteriaBuilder cb) {
						//创建Predicate对象
						Predicate pre = cb.conjunction();
						//创建存储SQL片段的集合
						List<Expression<Boolean>> sqlList = pre.getExpressions();
						if (!StringUtils.isEmpty(users.getLoginname())) {
							sqlList.add(cb.like(root.get("loginname"), "%"+ users.getLoginname() +"%"));
						}
						if (!StringUtils.isEmpty(users.getLslockout())) {
							sqlList.add(cb.equal(root.get("lslockout"), users.getLslockout()));
						}
						return pre;
					}
					
				};
		
				//分页参数
				Pageable pa = PageRequest.of(fy.getPage(), fy.getLimit());
				//分页查询对象
				Page<Users> result = ur.findAll(spec, pa);
				//获取layui参数
				List<Users> data = result.getContent();	//总数据
				System.out.println(data);
				long count = result.getTotalElements();	//总条数
				//设置layui参数
				if (data.size() > 0) {
					fy.setCode(0);
					fy.setData(data);
					fy.setCount(count);
				} else {
					fy.setCode(1);
					fy.setMsg("找不到数据");
				}
				return fy;
	}

	
	
	@Override
	public Integer getDelete(String uid) {
		return ur.getDelete(uid);
	}

	@Override
	public Integer delStudent(List<String> uid) {
		return ur.delStudent(uid);
	}

	@Override
	public Map<String, Object> add(Users user) {
		Map<String, Object> map = new HashMap<>();	//存储返回值
		Users usByLoginName = ur.findByLoginname(user.getLoginname());	//根据登录名查询是否有该用户
		if (usByLoginName != null) {
			map.put("code", 0);
			map.put("msg", "用户名已存在");
			return map;
		}
		MD5Util md5 = new MD5Util();	//实例化md5加密类
		String md5Pwd = user.getPassword();	//获取密码参数
		user.setPassword(md5.getMD5Pwd(md5Pwd));	//设置为md5加密后的形式
		GetDateUtils date = new GetDateUtils();	//实例化获取时间的工具类
		user.setCreattime(date.getTime());	//设置当前时间
		user.setLslockout("否");	//默认不锁定
		user.setPsdwrongtime(0);	//设置密码错误次数为0
		Users us = ur.save(user);	//执行添加操作
		if (us != null) {	//判断是否添加成功
			map.put("code", 1);
			map.put("msg", "添加成功");
			return map;
		}
		map.put("code", 0);
		map.put("msg", "添加失败");
		return map;
	}

	
	/**
	 * 修改是否锁定
	 */
	@Override
	public Integer editIslockout(String uid, String lslockout) {
		GetDateUtils date = new GetDateUtils();	//实例化获取时间的工具类
		if ("是".equals(lslockout)) {
			String dateTime = date.getTime();
			return ur.updateLslockout(uid, lslockout, dateTime);
		}
		int psdwrongtime = 0; //重置密码错误次数
		ur.updatePsdwrongtime(uid, psdwrongtime);	//修改密码错误次数
		String dateTime = "";
		return ur.updateLslockout(uid, lslockout, dateTime);
	}
	

}
